<?php
/**
* @file $Id: Search.php 570 2007-06-05 08:07:25Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

if($_REQUEST['modname']=='Students/Search.php' && AllowUse('Students/Student.php'))
{
	$modcat = 'Students';
	$_REQUEST['modname'] = 'Students/Student.php';
	Widgets('all');
	include($staticpath.'modules/'.$_REQUEST['modname']);
}
elseif(AllowUse('Students/Student.php'))
{
	if($_FOCUS['modules_search'] && $extra['force_search'])
		$_REQUEST['search_modfunc'] = '';
	
	if(($_REQUEST['search_modfunc']=='search' || !$_REQUEST['search_modfunc']))
	{
		if($_SESSION['student_id'] && User('PROFILE')!='parent' && User('PROFILE')!='student' && ($_REQUEST['modname']!='Students/Search.php' || $_REQUEST['student_id']=='new'))
		{
			$_SESSION['student_id'] = '';
			unset($_SESSION['student_id']);
			if(Preferences('MENU')=='Side')
				echo '<script language=JavaScript>parent.side.location="'.$_SESSION['Side_PHP_SELF'].'?modcat="+parent.side.document.forms[0].modcat.value;</script>';
		}

		$tmp_REQUEST = $_REQUEST;
		unset($tmp_REQUEST['expanded_view']);
		unset($tmp_REQUEST['search_modfunc']);
			
		if(!$_REQUEST['_FOCUS_PDF'])
		{
			if(!$_REQUEST['search_modfunc'])
			{
				$_REQUEST['search_modfunc'] = 'search';
				$unset_search_modfunc = true;
			}
			
			$programs = array(PreparePHP_SELF($tmp_REQUEST).'&amp;search_modfunc=search'=>_('Search Screen'),PreparePHP_SELF($tmp_REQUEST).'&amp;search_modfunc=list'=>_('Simple List'),PreparePHP_SELF($tmp_REQUEST) . "&amp;search_modfunc=list&amp;expanded_view=true"=>_('Customized List'),'Modules.php?modname=Users/Preferences.php&tab=student_fields&return=list'=>_('Customize Student List'));
			foreach($programs as $modname=>$title)
			{
	
				if(PreparePHP_SELF()!==$modname)
				{
					$tabcolor = 'DFDFDF';
					$textcolor = '666666';
				}
				else
				{
					$tabcolor = Preferences('HIGHLIGHT');
					$textcolor = 'FFFFFF';
				}
		
				$header .= '<TD width=10></TD><TD>'.DrawTab($title,$modname,$tabcolor,$textcolor,'_circle',array('tabcolor'=>Preferences('HIGHLIGHT'),'textcolor'=>'FFFFFF')).'</TD>';
			}
			
			if(!$extra['force_search'])
				echo TabsHeader($header);
			if($unset_search_modfunc===true)
				unset($_REQUEST['search_modfunc']);
		}

		switch(User('PROFILE'))
		{
			case 'parent':
			case 'student':
				if($_REQUEST['modname']=='Students/Search.php')
					$_REQUEST['search_modfunc']='list';
				else
				{
					echo '<BR>';
					PopTable('header',_('Search'));
					echo "<FORM action=Modules.php?modname=$_REQUEST[modname]&modfunc=$_REQUEST[modfunc]&search_modfunc=list&next_modname=$_REQUEST[next_modname]".$extra['action']." method=POST>";
					echo '<TABLE border=0>';
					if($extra['search'])
						echo $extra['search'];
					echo '<TR><TD colspan=2 align=center>';
					echo '<BR>';
					echo Buttons(_('Submit'),_('Reset'));
					echo '</TD></TR>';
					echo '</TABLE>';
					echo '</FORM>';
					PopTable('footer');
				}
			break;
	
			default;
				echo '<BR>';
				if($_REQUEST['modname']=='Students/Search.php')
					Widgets('all');
				$_SESSION['Search_PHP_SELF'] = PreparePHP_SELF($_SESSION['_REQUEST_vars']);
				echo '<script language=JavaScript>parent.help.location.reload();</script>';
				//if(Preferences('MENU')=='Top' && !$extra['second_col'])
				//	$width = 'width=80%';
				//else
				//	$width = 'width=99%';

				if(!$extra['search_title'])
					$extra['search_title'] = _('Student Search');
				PopTable('header',$extra['search_title'],$width);
				echo "<FORM action=Modules.php?modname=$_REQUEST[modname]&modfunc=$_REQUEST[modfunc]&search_modfunc=list&next_modname=$_REQUEST[next_modname]".$extra['action']." method=POST>";
				echo '<TABLE width=100%><TR><TD valign=top>';

				if($_FOCUS['direction']=='RTL')
					echo '<DIV id=menu_visiblesearch><BR><TABLE><TR><TD align='.ALIGN_RIGHT.'>'._('Student').'</TD><TD><INPUT type=text name=smart_name size=40></TD></TR></TABLE><BR><TABLE><TR><TD align='.ALIGN_RIGHT.' width=15></TD><TD><IMG SRC=assets/arrow_left.gif height=15></TD><TD><A HREF=# onclick=\'document.getElementById("menu_hiddensearch").style.display="inline";document.getElementById("menu_visiblesearch").style.display="none";\'><font color=gray><small><B>'._('More Search Options').'</B></small></font></A></TD></TR></TABLE></DIV>';
				else
					echo '<DIV id=menu_visiblesearch><BR><TABLE><TR><TD align='.ALIGN_RIGHT.'>'._('Student').'</TD><TD><INPUT type=text name=smart_name size=40></TD></TR></TABLE><BR><TABLE><TR><TD align='.ALIGN_RIGHT.' width=15></TD><TD><IMG SRC=assets/arrow_right.gif height=15></TD><TD><A HREF=# onclick=\'document.getElementById("menu_hiddensearch").style.display="inline";document.getElementById("menu_visiblesearch").style.display="none";\'><font color=gray><small><B>'._('More Search Options').'</B></small></font></A></TD></TR></TABLE></DIV>';

				echo '<DIV id=menu_hiddensearch style="display:none;">';
				Search('general_info');
				if($extra['search'])
					echo $extra['search'];
				Search('student_fields');
				echo '<CENTER><INPUT type=submit value="'._('Search').'"></CENTER>';
				echo '</DIV>';
				if($extra['second_col'])
					echo '</TD><TD valign=top>'.$extra['second_col'];
				else
					echo '</TD><TD valign=top>';
				echo '<TABLE><TR><TD width=100> &nbsp; </TD><TD><BR>';
				if(User('PROFILE')=='admin')
					echo '<INPUT type=checkbox name=_search_all_schools value=Y'.(Preferences('DEFAULT_ALL_SCHOOLS')=='Y'?' CHECKED':'').'><font color=black>'._('Search All Schools').'</font><BR>';
				echo '<INPUT type=checkbox name=include_inactive value=Y><font color=black>'._('List Inactive Students').'</font><BR>';
				echo '</TD></TR></TABLE>';
				echo '<TABLE><TR><TD width=75></TD><TD>';
				echo Buttons(_('Search'),_('Reset'));
				echo '</TD></TR></TABLE>';
				echo '</TD></TR></TABLE>';
				echo '</FORM>';
				PopTable('footer');
			break;
		}
	}
	//if($_REQUEST['search_modfunc']=='list')
	elseif($_REQUEST['search_modfunc']!='result')
	{
		if(!$_REQUEST['include'])
			$_REQUEST['include'] = 'General_Info';
		if(!$_REQUEST['next_modname'] || $_REQUEST['next_modname']=='Students/Search.php')
			$_REQUEST['next_modname'] = 'Students/Student.php';
	
		if($_REQUEST['expanded_view']!=='true' && $_REQUEST['expanded_view']!=='false' && Preferences('DEFAULT_CUSTOMIZED_LIST')=='Y' && count($extra['columns_after'])<=3 && !count($extra['columns_before']))
		{
			unset($_REQUEST['expanded_view']);
			$_REQUEST['expanded_view'] = 'true';
		}
		elseif($_REQUEST['expanded_view']!=='true')
			$_REQUEST['expanded_view'] = 'false';
		
		$students_RET = GetStuList($extra);
		if($extra['array_function'] && function_exists($extra['array_function']))
			$students_RET = $extra['array_function']($students_RET);
			
		if($_REQUEST['modname']=='Attendance/DailySummary.php' && $_REQUEST['_FOCUS_PDF'] && $_REQUEST['LO_save']!='1')
			$student_id_title = _('Stu ID');
		else
			$student_id_title = _('Student ID');
		$LO_columns = array('FULL_NAME'=>_('Student'));
		if(Preferences('DISPLAY_STUDENT_ID')=='Y')
			$LO_columns['STUDENT_ID'] = $student_id_title;
		if(Preferences('DISPLAY_GRADE')=='Y')
			$LO_columns['GRADE_ID'] = _('Grade');
		$name_link['FULL_NAME']['link'] = "Modules.php?modname=$_REQUEST[next_modname]";
		$name_link['FULL_NAME']['variables']['student_id']='STUDENT_ID';
		if($_REQUEST['modname']=='Students/Search.php' || $_REQUEST['modname']=='Students/Student.php' || $_REQUEST['modname']=='Scheduling/Schedule.php' || $_REQUEST['modname']=='Scheduling/Requests.php' || $_REQUEST['modname']=='Grades/StudentRCGrades.php' || $_REQUEST['modname']=='Attendance/StudentSummary.php' || $_REQUEST['modname']=='Eligibility/Student.php' || $_REQUEST['modname']=='Students/Student.php?include=Comments')
			$name_link['FULL_NAME']['variables']['LO_index'] = 'index';

		if($_REQUEST['_search_all_schools'])
			$name_link['FULL_NAME']['variables']['school_id'] = 'SCHOOL_ID';	

		if(is_array($extra['link']['FULL_NAME']))
		{
			$extra['link']['FULL_NAME']['variables'] = $extra['link']['FULL_NAME']['variables'] + $name_link['FULL_NAME']['variables'];
			if($extra['link']['FULL_NAME']!==false && !$extra['link']['FULL_NAME'])
				$extra['link']['FULL_NAME']['link'] = $name_link['FULL_NAME']['link'];
			$link = $extra['link'];
		}
		elseif($extra['link']['FULL_NAME']!==false)
			$link = $name_link;

		if($extra['link']['FULL_NAME']!==false)
			$link['FULL_NAME']['link'] .= '&search_modfunc=result';
	
		if(is_array($extra['columns_before']))
		{
			$columns = $extra['columns_before'] + $LO_columns;
			$LO_columns = $columns;
		}
		if(is_array($extra['columns_after']))
			$columns = $LO_columns + $extra['columns_after'];
		if(!$extra['columns_before'] && !$extra['columns_after'])
			$columns = $LO_columns;
	
		if($columns>15 && $_REQUEST['_FOCUS_PDF'] && $_REQUEST['LO_save']!='1')
		{
			unset($columns['GRADE_ID']);
			//unset($columns['STUDENT_ID']);
		}

		if(count($students_RET) > 1 || $link['add'] || !$link['FULL_NAME'] || $extra['columns_before'] || $extra['columns_after'] || ($extra['BackPrompt']==false && count($students_RET)==0) || ($extra['Redirect']===false && count($students_RET)==1))
		{
			if($_REQUEST['modname']=='Students/Search.php')
				DrawHeader(_('Choose a Student'));
	
			$tmp_REQUEST = $_REQUEST;
			unset($tmp_REQUEST['expanded_view']);
			unset($tmp_REQUEST['search_modfunc']);
			
			if(!$_REQUEST['_FOCUS_PDF'])
			{
				// so that PreparePHP_SELF gets these two variables at the end of the URL
				if($_REQUEST['search_modfunc']=='list')
				{
					unset($_REQUEST['search_modfunc']);
					$_REQUEST['search_modfunc'] = 'list';
				}
				if($_REQUEST['expanded_view']=='true')
				{
					unset($_REQUEST['expanded_view']);
					$_REQUEST['expanded_view'] = 'true';
				}
				if($_REQUEST['expanded_view']==='false')
				{
					unset($_REQUEST['expanded_view']);
					$_REQUEST['expanded_view'] = 'false';
				}

				$programs = array(PreparePHP_SELF($tmp_REQUEST).'&amp;search_modfunc=search'=>_('Search Screen'),PreparePHP_SELF($tmp_REQUEST).'&amp;search_modfunc=list&amp;expanded_view=false'=>_('Simple List'),PreparePHP_SELF($tmp_REQUEST) . "&amp;search_modfunc=list&amp;expanded_view=true"=>_('Customized List'),'Modules.php?modname=Users/Preferences.php&tab=student_fields&return=list'=>_('Customize Student List'));
				foreach($programs as $modname=>$title)
				{
		
					if(PreparePHP_SELF()!==$modname)
					{
						$tabcolor = 'DFDFDF';
						$textcolor = '666666';
					}
					else
					{
						$tabcolor = Preferences('HIGHLIGHT');
						$textcolor = 'FFFFFF';
					}
			
					$header .= '<TD width=10></TD><TD>'.DrawTab($title,$modname,$tabcolor,$textcolor,'_circle',array('tabcolor'=>Preferences('HIGHLIGHT'),'textcolor'=>'FFFFFF')).'</TD>';
				}
				
				if(!$_REQUEST['_FOCUS_PDF'])
					echo TabsHeader($header);
			}
/*			if($_REQUEST['expanded_view']!='true')
				DrawHeader("<A HREF=".PreparePHP_SELF($tmp_REQUEST) . "&expanded_view=true>Expanded View</A>");
			else
				DrawHeader("<A HREF=".PreparePHP_SELF($tmp_REQUEST) . "&expanded_view=false>Original View</A>");
*/
			DrawHeader(str_replace('<BR>','<BR> &nbsp;',substr($_FOCUS['SearchTerms'],0,-4)));
			if($_REQUEST['LO_save']!='1')
			{
				$_SESSION['List_PHP_SELF'] = PreparePHP_SELF($_SESSION['_REQUEST_vars']);
				echo '<script language=JavaScript>parent.help.location.reload();</script>';
			}
			if(!$extra['singular'] || !$extra['plural'])
			{
				$extra['singular'] = _('Student');
				$extra['plural'] = _('Students');
			}
			ListOutput($students_RET,$columns,$extra['singular'],$extra['plural'],$link,$extra['LO_group'],$extra['options']);
		}
		elseif(count($students_RET)==1)
		{
			echo '<script language=javascript>document.getElementById("top_frame").style.position="relative";</script>';
			if(count($link['FULL_NAME']['variables']))
			{
				foreach($link['FULL_NAME']['variables'] as $var=>$val)
					$_REQUEST[$var] = $students_RET['1'][$val];
			}
			if(!is_array($students_RET[1]['STUDENT_ID']))
			{
				$_SESSION['student_id'] = $students_RET[1]['STUDENT_ID'];
				$_SESSION['UserSchool'] = $students_RET[1]['SCHOOL_ID'];
				if(Preferences('MENU')=='Side')
					echo '<script language=JavaScript>parent.side.location="'.$_SESSION['Side_PHP_SELF'].'?modcat="+parent.side.document.forms[0].modcat.value;</script>';
				unset($_REQUEST['search_modfunc']);
			}
			if($_REQUEST['modname']!=$_REQUEST['next_modname'])
			{
				$modname = $_REQUEST['next_modname'];
				if(strpos($modname,'?'))
					$modname = substr($_REQUEST['next_modname'],0,strpos($_REQUEST['next_modname'],'?'));
				if(strpos($modname,'&'))
					$modname = substr($_REQUEST['next_modname'],0,strpos($_REQUEST['next_modname'],'&'));
				if($_REQUEST['modname'])
					$_REQUEST['modname'] = $modname;
				include('modules/'.$modname);
			}
		}
		else
			BackPrompt(_('No Students were found.'));
	}
}
?>